Set data file names:

# data file names:
data_file <- "../../clean_data/StopData_merged2.rds" # use the cleaned version of StopData_merged.rds for plotting. See StopData_clean_merged_rds.R for more information.
census_tract_folder <- "../../raw_data/Census_Tract_Polygons2010"
census_tract_shp <- "Census_tracts_2010"
census_tract_csv <- "../../raw_data/Census_tracts_2010.csv"
alameda_xls <- "../../raw_data/2010_Pop_Block_County.xls"
alameda_tract_nums_xls <- "../../raw_data/census_tract_numbers.xls"
alameda_clean_csv <- "../../raw_data/alameda_clean_census.csv"
berkeley_clean_csv <- "../../clean_data/berkeley_census_clean.csv"

Read in the data:

# read in the data to modify:
df <- readRDS( data_file )
berkpop <- read.csv2(berkeley_clean_csv)
berkpop <- berkpop[,-1]
a3 <- read.csv2(alameda_clean_csv)
a3 <- a3[,-1]
# Location info (to set lim values for coordinates):
latmax <- max( df$lat, na.rm = TRUE )
latmin <- min( df$lat, na.rm = TRUE )
lonmax <- max( df$long, na.rm = TRUE )
lonmin <- min( df$long, na.rm = TRUE )
latvals <- c( latmin, latmax )
lonvals <- c( lonmin, lonmax )

Get the regular Berkeley map:

berkgg <-ggmap(berkMap) +
  xlim(-122.335, lonmax) + ylim(latvals) +
  ditch_the_axes 
## Scale for 'x' is already present. Adding another scale for 'x', which
## will replace the existing scale.
## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.
berkgg

Get the census shape files:

Trying some things:

Examine total population by census block:

# READ IN DATA RIGHT!!!
census.df <- fortify(censusT, region = 'GEOID10')
pop <- read.csv(file=census_tract_csv, header = TRUE)
names(pop)[13] <- "id"
pop <- pop %>%
  select(GEOID10, id, TotalPop) %>% 
  mutate( id = str_c("0", as.character(id)) ) 
census.df_pop <- left_join(census.df, pop, by = "id")
popdenmap <- ggmap(berkMap) + 
  geom_polygon(data = census.df_pop, aes(x = long, y = lat, group = group, fill = TotalPop), color = "white", alpha = .7) 
popdenmap

All BPD Stops Density, 2015-2016

# a contour plot
berkgg +
  stat_density2d(aes(x = long, y = lat, fill= ..level.., alpha = .2* ..level..),
                 size = 2, bins = 5, data = df, geom = "polygon") +
  scale_fill_gradient(low = "black", high = "red") +
  theme (panel.grid.major = element_blank (), # remove major grid
         panel.grid.minor = element_blank ()  # remove minor grid
  )+ 
  ggtitle("All BPD Stops Density, 2015-2016") +
  labs(alpha = element_blank())+
  guides(alpha = FALSE)

Works, but no google maps:

Cleaning the Berkeley population info (do not run):

Population of black people by Census Block

black1 <- berkgg + 
  geom_polygon(data = census.df, aes(x = long, y = lat, group = group, fill = Black.or.African.American), color = "white", alpha = .7) 

Population density of black people within a census block:

black2 <- berkgg + 
  geom_polygon(data = census.df, aes(x = long, y = lat, group = group, fill = Percent.Black), color = "white", alpha = .7) 

View side by side:

black1

black2